Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.
To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store.
Declaring:-
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
int myNum[3] = {10, 20, 30};
Access elements of the array:-
You access an array element by referring to the index number.
This statement accesses the value of the first element in cars
Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
Change elements of the array:-
To change the value of a specific element, refer to the index number
Looping through an array:-
You can loop through the array elements with the for loop.
Omiting Array size:-
You don't have to specify the size of the array.
But if you don't, it will only be as big as the elements that are inserted into it.
string cars[] = {"Volvo", "BMW", "Ford"};
// size of array is always 3
This is completely fine. However, the problem arise if you want extra space for future elements. Then you have to overwrite the existing values
If you specify the size however, the array will reserve the extra space
string cars[5] = {"Volvo", "BMW", "Ford"};
// size of array is 5, even though it's only three elements inside it
Now you can add a fourth and fifth element without overwriting the others
It is also possible to declare an array without specifying the elements on declaration,
and add them later